package com.easyjava.builder;

import com.easyjava.bean.Constants;
import com.easyjava.utils.MyStringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import static com.easyjava.init.InitCascade.*;

/**
 * @Author: 小闹心
 * @Description:
 * @DateTime: 2025/6/19 19:56
 **/
public class BuildVo {
    public static void execute(String[] bd){
        String[] cascadeTable = bd[0].split(",");
        List<String> cascadeTables = CASCADE_TABLE_MAP.get(cascadeTable[0]);
        if (cascadeTables == null) {
            cascadeTables = new ArrayList<>();
        }
        cascadeTables.add(bd[0]);
        CASCADE_TABLE_MAP.put(cascadeTable[0], cascadeTables);
        CASCADE_FIELD_RELATION_MAP.put(bd[0], bd[1]);
        String[] fieldsAndRelation = bd[1].split(" ");
        String[] relations = fieldsAndRelation[1].split("\\|");
        File folder = new File(Constants.PATH_VO);
        if(!folder.exists()){
            folder.mkdirs();
        }
        StringBuilder className = new StringBuilder();
        className.append(MyStringUtils.upperCaseFirstLetter(cascadeTable[0]));
        if(cascadeTable.length > 1){
            className.append("And");
        }
        for(int i = 1; i < cascadeTable.length;i++){
            className.append(MyStringUtils.upperCaseFirstLetter(TABLE_INFO_MAP.get(cascadeTable[i]).getBeanName()));
            if(i != cascadeTable.length - 1){
                className.append("And");
            }
        }
        File file = new File(folder, className + ".java");
        OutputStream out = null;
        OutputStreamWriter outWriter = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(file);
            outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outWriter);
            bw.write("package " + Constants.PACKAGE_VO + ";");
            bw.newLine();
            bw.newLine();
            for(String table : cascadeTable){
                bw.write("import " + Constants.PACKAGE_PO + "." + MyStringUtils.upperCaseFirstLetter(TABLE_INFO_MAP.get(table).getBeanName()) + ";");
                bw.newLine();
            }
            if (fieldsAndRelation[1].contains("-n") || fieldsAndRelation[1].contains("-m")) {
                bw.write("import java.util.List;");
                bw.newLine();
            }
            bw.newLine();
            // 生成类注释
            BuildComment.createClassComment(bw,className + "联合展示表");
            bw.write("public class " + className + " extends "
                    + MyStringUtils.upperCaseFirstLetter(TABLE_INFO_MAP.get(cascadeTable[0]).getBeanName()) + "{ ");
            bw.newLine();
            for(int i = 1; i < cascadeTable.length;i++){
                String[] oneAndTwo = relations[i - 1].split("-");
                if("n".equals(oneAndTwo[1]) || "m".equals(oneAndTwo[1])){
                    bw.write("\t private List<"
                            + MyStringUtils.upperCaseFirstLetter(TABLE_INFO_MAP.get(cascadeTable[i]).getBeanName())
                            + "> "
                            + MyStringUtils.lowerCaseFirstLetter(TABLE_INFO_MAP.get(cascadeTable[i]).getBeanName()) + ";");
                }else{
                    bw.write("\t private "
                            + MyStringUtils.upperCaseFirstLetter(TABLE_INFO_MAP.get(cascadeTable[i]).getBeanName())
                            + " "
                            + MyStringUtils.lowerCaseFirstLetter(TABLE_INFO_MAP.get(cascadeTable[i]).getBeanName()) + ";");
                }

                bw.newLine();
            }

            bw.write("}");
            bw.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outWriter != null){
                try {
                    outWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
